# 呼叫相关巨人
import pygame
import os
import sys
from pygame.locals import *
# 初始化pygame
pygame.init()
# 游戏时钟，用于更新屏幕
fpsClock = pygame.time.Clock()
# 定义游戏屏幕大小
win = pygame.display.set_mode((800, 480))
# 获得背景颜色
bg = pygame.image.load('bg.jpg')
standing = pygame.image.load('standing.png')
x = 400-(standing.get_rect().width/2)
y = (480-(standing.get_rect().height))
rImage = [pygame.image.load('Left1.png'),
          pygame.image.load('Left2.png'),
          pygame.image.load('Left3.png'),
          pygame.image.load('Left4.png'),
          pygame.image.load('Left5.png'),
          pygame.image.load('Left6.png'),
          pygame.image.load('Left7.png'),
          pygame.image.load('Left8.png'),
          pygame.image.load('Left9.png')]
lImage = [pygame.image.load('Right1.png'),
          pygame.image.load('Right2.png'),
          pygame.image.load('Right3.png'),
          pygame.image.load('Right4.png'),
          pygame.image.load('Right5.png'),
          pygame.image.load('Right6.png'),
          pygame.image.load('Right7.png'),
          pygame.image.load('Right8.png'),
          pygame.image.load('Right9.png')]
pygame.mixer.init()
bgMusic = pygame.mixer.Sound("background.ogg")
bgMusic.play(-1)
ri=0
li=0
# 一直更新屏幕
while True:
    o = 0
    k = 0
# 开始绘制新的屏幕内容
    win.blit(bg,(0,0))
    # 侦测用户的交互动作
    for event in pygame.event.get():
        keys=pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            x+=-5
            o=1
        elif keys[pygame.K_RIGHT]:
            x+=5
            k=1
        elif keys[pygame.K_UP]:
            y+=-5
        elif keys[pygame.K_DOWN]:
            y+=5
    if o==1:
        win.blit(rImage[ri], (x, y))
        ri += 1
        if ri == 9:
            ri -= 9
    elif k==1:
        win.blit(lImage[li], (x, y))
        li += 1
        if li == 9:
            li -= 9
    else:
        win.blit(standing, (x, y))
        # 如果退出游戏
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    # 完成屏幕绘制，执行更新
    pygame.display.update()
    # 指定刷新速率：每秒30帧
    # 如遇到复杂绘制运算，要降低速率
    fpsClock.tick(30)
